import {createSlice} from "@reduxjs/toolkit";

const carSlice = createSlice({
    // 切片的名字
    name: 'car',
    // 切片的状态
    initialState: {
        carList: []
    },
    // 切片的同步方法
    reducers: {
        addCar(state, {payload}) {
            state.carList.unshift({
                id: Math.random().toString(36).slice(2),
                ...payload
            })
        },
        remoCar(state, {payload}) {
            state.carList = state.carList.filter(item => item.id !== payload)
        }
    }
})
// 修改切片中的数据: 切片函数生成 action 对象，接收的参数即是 payload 属性值
const {addCar, remoCar} = carSlice.actions
export default carSlice
export {
    addCar,
    remoCar
}